using System;
using System.Windows.Forms;
using Damany.RemoteImaging.Common.Forms;

namespace RemoteImaging
{
    public class VisualStateManager : IVisualStateManager
    {
        private Form _activeForm;
        private ProgressForm _busyIndicator = null;
        private bool _isBusy = false;

        public VisualStateManager()
        {

        }
        public string Message { get; set; }


        public bool IsBusy
        {
            set
            {
                if (_isBusy == value)
                    return;

                _activeForm = FormHelper.GetActiveForm();
                Action<bool> action = ShowBusyIndicator;

                if (value == true)
                    _activeForm.BeginInvoke(action, value);
                else
                    _activeForm.Invoke(action, value);

                _isBusy = value;
            }
        }


        private void ShowBusyIndicator(bool isbusy)
        {

            if (isbusy)
            {
                if (_busyIndicator == null)
                {
                    _busyIndicator = new ProgressForm();
                    _busyIndicator.Text = _activeForm.Text;
                    _busyIndicator.ShowDialog(_activeForm);
                }

            }
            else
            {

                if (_busyIndicator != null)
                {
                    _busyIndicator.Close();
                    _busyIndicator = null;
                }
            }
        }
    }
}
